package com.onlinelibrary.activities.north;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.onlinelibrary.R;
import com.onlinelibrary.activities.App;
import com.onlinelibrary.activities.map.MapActivity;
import com.onlinelibrary.databinding.ActivityNorthBinding;
import com.onlinelibrary.utils.Http;
import com.onlinelibrary.view.FallObject;
import com.onlinelibrary.view.FallingView;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static androidx.appcompat.content.res.AppCompatResources.getDrawable;

public class NorthVM extends AndroidViewModel {
    public static MutableLiveData<String> introduce=new MutableLiveData<>();
    public static MutableLiveData<FallObject> fall=new MutableLiveData<>();
    @SuppressLint("StaticFieldLeak")
    private static NorthActivity northActivity;
    public NorthVM(@NonNull Application application) {
        super(application);
        App.position="北馆";
        introduce.setValue("正在获取。。。");
    }
    public void setBinding(ActivityNorthBinding binding, NorthActivity northActivity) {
        NorthVM.northActivity=northActivity;
        fall.setValue(falling(northActivity));
        Http.get("position/getNorthDescript/", new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                introduce.postValue("正在获取。。。");
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                try {
                    introduce.postValue(response.body().string());
                } catch (Exception e) {
                    introduce.postValue("正在获取。。。");
                }
            }
        });
    }
    @BindingAdapter("bindFall")
    public static void bindFall(FallingView fallingView, MutableLiveData<FallObject> fall){
        if (fall!=null){
            fallingView.addFallObject(fall.getValue(),15);
        }
    }
    public FallObject falling(Context mcontext){
        int r = (new Random().nextInt());
        FallObject.Builder builder;
        if(r % 4 == 0){
            builder = new FallObject.Builder(getDrawable(mcontext, R.drawable.sun));
        }else if(r % 4 == 1){
            builder = new FallObject.Builder(getDrawable(mcontext,R.drawable.leaf));
        }else if(r%4 == 2){
            builder = new FallObject.Builder(getDrawable(mcontext,R.drawable.aut));
        }else{
            builder = new FallObject.Builder(getDrawable(mcontext,R.drawable.snow));
        }
        return builder.setSpeed(7, true).setSize(150, 150, true).setWind(true, false).build();
    }
    public static void zero(View view){
        App.floor="G";
        northActivity.startActivity(new Intent(northActivity, MapActivity.class));
    }
    public static void first(View view){
        App.floor="1";
        northActivity.startActivity(new Intent(northActivity, MapActivity.class));
    }
    public static void second(View view){
        App.floor="2";
        northActivity.startActivity(new Intent(northActivity, MapActivity.class));
    }
    public static void third(View view){
        App.floor="3";
        northActivity.startActivity(new Intent(northActivity, MapActivity.class));
    }
    public static void fourth(View view){
        App.floor="4";
        northActivity.startActivity(new Intent(northActivity, MapActivity.class));
    }
    public static void fifth(View view){
        App.floor="5";
        northActivity.startActivity(new Intent(northActivity, MapActivity.class));
    }
}
